package com.quasar.hpatient.view.personal.register.registerFragment;

import android.app.DatePickerDialog;
import android.support.v4.app.Fragment;
import android.widget.DatePicker;

import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseFragment;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.medicalmodel.PatientInfoData;
import com.quasar.hpatient.onInterface.viewInterface.UiInterface;
import com.quasar.hpatient.presenter.AloneUserPresenter;
import com.quasar.hpatient.utils.TimeZoneUtil;
import com.quasar.hpatient.view.personal.register.RegisterEditActivity;
import com.quasar.hpatient.widget.SelectWidget;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;

import java.util.Calendar;
import java.util.Date;

/**
 * author : 许建宏
 * Date: 2017/11/27 0027
 * View:手术时间
 */

@EFragment(R.layout.register_operationdate_fragment_layout)
public class RegisterOperationDateFragment extends BaseFragment  implements UiInterface<DataStateResult> {

    private AloneUserPresenter aloneUserPresenter;
    @ViewById(R.id.sw_operationday)
    SelectWidget sw_operationday;
    @FragmentArg
    PatientInfoData patient;


    @AfterViews
    void init(){
        initView();
        initData();
    }

    @Click(R.id.tv_next)
    public void tv_next(){

        String operationDate=sw_operationday.getText().toString().trim();

        if(operationDate==null || operationDate.length()==0){
           msg("请输入手术时间");
           return;
        }

        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append(operationDate);
        stringBuffer.append(" 00:00:00");

        aloneUserPresenter.AloneUserData(ConstantManger.USEROPERATIONDATE,stringBuffer.toString(),ConstantManger.TABLEOPERATION);

    }

    //选择时间
    @Click(R.id.sw_operationday)
    public void sw_operationday(){
        //选择手术日期
        Date date =new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i2, int i3) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.YEAR, i);
                calendar.set(Calendar.MONTH, i2);
                calendar.set(Calendar.DAY_OF_MONTH, i3);

                sw_operationday.setText("" + TimeZoneUtil.sdf.format(calendar.getTime()).substring(0,10));
            }
        }, year, month, day);

        DatePicker datePicker = datePickerDialog.getDatePicker();
        datePicker.setMaxDate(TimeZoneUtil.getStringToDate(TimeZoneUtil.sdf.format(new Date()), "yyyy-MM-dd"));
        datePickerDialog.setTitle("选择手术日期");
        datePickerDialog.show();
    }

    @Click(R.id.tv_last)
    public void tv_last(){
        RegisterEditActivity.ViewPage(5);
    }

    @Click(R.id.tv_skip)
    public void tv_skip(){
        RegisterEditActivity.ViewPage(7);
    }

    @Override
    public void initView() {

        if(patient.getPatientOperationInfo()!=null){
            if(patient.getPatientOperationInfo().getOperationTime()!=null && patient.getPatientOperationInfo().getOperationTime().length()>0){
                if(patient.getPatientOperationInfo().getLogTime().length()==19){
                    sw_operationday.setText(patient.getPatientOperationInfo().getOperationTime());
                }else{
                    sw_operationday.setText(patient.getPatientOperationInfo().getOperationTime().substring(0,10));
                }

            }
        }
    }

    @Override
    public void initData() {
        aloneUserPresenter=new AloneUserPresenter(this);
    }

    @Override
    public void onViewSuccess(DataStateResult data) {
        if(data.getMsgCode().equals("0x00000000")){
            RegisterEditActivity.ViewPage(7);
        }
    }

    @Override
    public void onViewError(String msg) {
        if(msg!=null){
            msg(msg);
        }
    }

    @Override
    public void onViewRefreshUI() {

    }
}
